using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProduceBasicData.Api.Application.Command.OutboundOrder;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.ErrorCode;

namespace ProduceBasicData.Api.Controllers
{
    /// <summary>
    /// 出货订单数据控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OutboundOrderController : ControllerBase
    {
        private readonly IMediator mediator;

        public OutboundOrderController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取出货订单数据接口
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns>出货订单数据</returns>
        [HttpGet]
        public async Task<ApiResult<OutboundOrderChartDto>> GetOutboundOrderData([FromQuery] GetOutboundOrderDataCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取出货订单数据接口（简化版本，无参数）
        /// </summary>
        /// <returns>出货订单数据</returns>
        [HttpGet]
        public async Task<ApiResult<OutboundOrderChartDto>> GetOutboundOrderDataSimple()
        {
            var request = new GetOutboundOrderDataCommand
            {
                TimePeriodType = 2, // 本月
                SortType = 2, // 按订单数量降序
                PageSize = 10,
                IncludeChartData = true,
                OnlyWithOrders = true
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取上月出货订单数据接口
        /// </summary>
        /// <returns>上月出货订单数据</returns>
        [HttpGet]
        public async Task<ApiResult<OutboundOrderChartDto>> GetLastMonthOutboundOrderData()
        {
            var request = new GetOutboundOrderDataCommand
            {
                TimePeriodType = 1, // 上月
                SortType = 2, // 按订单数量降序
                PageSize = 0, // 不分页
                IncludeChartData = true,
                OnlyWithOrders = true
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取近6个月出货订单数据接口
        /// </summary>
        /// <returns>近6个月出货订单数据</returns>
        [HttpGet]
        public async Task<ApiResult<OutboundOrderChartDto>> GetLastSixMonthsOutboundOrderData()
        {
            var request = new GetOutboundOrderDataCommand
            {
                TimePeriodType = 3, // 近6个月
                SortType = 2, // 按订单数量降序
                PageSize = 0, // 不分页
                IncludeChartData = true,
                OnlyWithOrders = true
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取自定义时间范围出货订单数据接口
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>自定义时间范围出货订单数据</returns>
        [HttpGet]
        public async Task<ApiResult<OutboundOrderChartDto>> GetCustomPeriodOutboundOrderData(DateTime startDate, DateTime endDate)
        {
            var request = new GetOutboundOrderDataCommand
            {
                TimePeriodType = 4, // 自定义
                StartDate = startDate,
                EndDate = endDate,
                SortType = 2, // 按订单数量降序
                PageSize = 0, // 不分页
                IncludeChartData = true,
                OnlyWithOrders = true
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取销售出库订单数据接口
        /// </summary>
        /// <returns>销售出库订单数据</returns>
        [HttpGet]
        public async Task<ApiResult<OutboundOrderChartDto>> GetSalesOutboundOrderData()
        {
            var request = new GetOutboundOrderDataCommand
            {
                TimePeriodType = 2, // 本月
                OutType = 2, // 销售出库
                SortType = 2, // 按订单数量降序
                PageSize = 0, // 不分页
                IncludeChartData = true,
                OnlyWithOrders = true
            };
            return await mediator.Send(request);
        }
    }
}





